package com.sailfish.springbootdemo.dao.db2;

import com.sailfish.springbootdemo.common.DB2TableName;
import com.sailfish.springbootdemo.pojo.db2.PcbCapacityData;
import com.sailfish.springbootdemo.pojo.db2.PcbMaterialFactory;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbMaterialFactoryDao {

    @Results(id = "pcbMaterialFactoryResultMap", value = {
            @Result(column = "pcb_factory_material_id", property = "pcbFactoryMaterialId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcb_material_library_and_factory_id", property = "pcbMaterialLibraryAndFactoryId", jdbcType = JdbcType.INTEGER),
            @Result(column = "factory_id", property = "factoryId", jdbcType = JdbcType.INTEGER),
            @Result(column = "row_key", property = "rowKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "version", property = "version", jdbcType = JdbcType.VARCHAR),
            @Result(column = "material_status", property = "materialStatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "approver", property = "approver", jdbcType = JdbcType.VARCHAR),
            @Result(column = "open_status", property = "openStatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_user", property = "createUser", jdbcType = JdbcType.VARCHAR),
            @Result(column = "update_user", property = "updateUser", jdbcType = JdbcType.VARCHAR),
    })

    /*
     * 查询单个
     */
    @Select("<script> " +
            "select * " +
            "from " + DB2TableName.PCB_MATERIAL_FACTORY +
            "<where> " +
            "<if test='id != null'>" +
            " and pcb_factory_material_id = #{id}" +
            "</if>" +
            "</where> " +
            " order by sort" +
            "</script>")
    PcbMaterialFactory getById(@Param("id") Integer id);

    @Select("<script> " +
            "select * " +
            "from " + DB2TableName.PCB_MATERIAL_FACTORY +
            "<where> " +
            "<if test='pcbMaterialLibraryAndFactoryId != null'>" +
            " and pcb_material_library_and_factory_id = #{pcbMaterialLibraryAndFactoryId}" +
            "</if>" +
            "</where> " +
            " order by sort" +
            "</script>")
    List<PcbMaterialFactory> getByMaterialLibraryAndFactoryId(@Param("pcbMaterialLibraryAndFactoryId") Integer pcbMaterialLibraryAndFactoryId);

    /*
     * 查询行
     */
    @Select("<script> " +
            "select * " +
            "from " + DB2TableName.PCB_MATERIAL_FACTORY +
            "<where> " +
            "<if test='rowKey != null'>" +
            " and row_key = #{rowKey}" +
            "</if>" +
            "<if test='materialStatus != null'>" +
            " and material_status = #{materialStatus}" +
            "</if>" +
            "<if test='openStatus != null'>" +
            " and (open_status <![CDATA[<=]]> #{openStatus})" +
            "</if>" +
            "<if test='factoryId != null'>" +
            " and factory_id = #{factoryId}" +
            "</if>" +
            "<if test='approveUserId != null'>" +
            " and approver = #{approveUserId}" +
            "</if>" +
            "</where> " +
            " order by sort" +
            "</script>")
    @ResultMap(value = "pcbMaterialFactoryResultMap")
    List<PcbMaterialFactory> getByLimit(@Param("rowKey") String rowKey, @Param("factoryId") Integer factoryId,
                                        @Param("materialStatus") Integer materialStatus, @Param("openStatus") Integer openStatus,
                                        @Param("approveUserId") Integer approveUserId);

    @Select("<script> " +
            "select row_key " +
            "from " + DB2TableName.PCB_MATERIAL_FACTORY +
            "<where> " +
            "<if test='rowKey != null'>" +
            " and row_key = #{rowKey}" +
            "</if>" +
            "<if test='materialStatus != null'>" +
            " and material_status = #{materialStatus}" +
            "</if>" +
            "<if test='openStatus != null'>" +
            " and open_status = #{openStatus}" +
            "</if>" +
            "<if test='factoryId != null'>" +
            " and factory_id = #{factoryId}" +
            "</if>" +
            "</where> " +
            " order by sort" +
            "</script>")
    List<String> getRowKeyByLimit(@Param("rowKey") String rowKey, @Param("factoryId") Integer factoryId, @Param("materialStatus") Integer materialStatus, @Param("openStatus") Integer openStatus);

    /*
     *新增所有列
     */
    @Insert("insert into " + DB2TableName.PCB_MATERIAL_FACTORY + " (pcb_material_library_and_factory_id, factory_id, row_key, version, material_status, approver, open_status, remark, create_time, create_user, update_time, update_user, status, sort) values " +
            "(#{pcbMaterialLibraryAndFactoryId}, #{factoryId}, #{rowKey}, #{version}, #{materialStatus}, #{approver}, #{openStatus}, #{remark}, #{createTime}, #{createUser}, #{updateTime}, #{updateUser}, #{status}, #{sort})")
    @Options(useGeneratedKeys = true, keyProperty = "pcbFactoryMaterialId")
    Integer insert(PcbMaterialFactory param);

    @Update("<script>" +
            "update " + DB2TableName.PCB_MATERIAL_FACTORY +
            "<set>" +
            "<if test='materialStatus != null'>" +
            "material_status = #{materialStatus}," +
            "</if>" +
            "<if test='approver != null'>" +
            "approver = #{approver}," +
            "</if>" +
            "<if test='remark != null'>" +
            "remark = #{remark}," +
            "</if>" +
            "<if test='openStatus != null'>" +
            "open_status = #{openStatus}," +
            "</if>" +
            "</set>" +
            "where pcb_factory_material_id = #{pcbFactoryMaterialId}" +
            "</script>")
    Integer updateMaterialStatus(@Param("pcbFactoryMaterialId") Integer pcbFactoryMaterialId,
                                 @Param("approver") String approver, @Param("materialStatus") Integer materialStatus,
                                 @Param("remark") String remark, @Param("openStatus") Integer openStatus);

    /*
     *通过rowKey和factoryId删除
     */
    @Delete("delete from " + DB2TableName.PCB_MATERIAL_FACTORY + " where pcb_material_library_and_factory_id = #{pcbMaterialLibraryAndFactoryId} ")
    Integer deleteByMaterialLibraryAndFactoryId(@Param("pcbMaterialLibraryAndFactoryId") Integer pcbMaterialLibraryAndFactoryId);
}
